import { createStore } from 'vuex'
import axios from 'axios'

export interface UserProps {
  isLogin: boolean;
  name?: string;
  id?: string;
}

export interface OrderProps {
  data: OrdersProps[];
  currentPage: number;
  // pages: number;
  total: number;
}

export interface OrdersProps {
  id: string;
  orderNumber: string;
  orderTypeName: string;
  orderTypeCode: string;
  consigneeName: string;
  [key: string]: any;
}

export interface VuxProps {
  user: UserProps;
  order: OrderProps;
}
export default createStore<VuxProps>({
  state: {
    user: {
      isLogin: false
    },
    order: {
      data: [],
      currentPage: 0,
      total: 0,
    }
  },
  mutations: {
    login(state) {
      state.user = { ...state.user, isLogin: true, name: 'AAAA', id: '11112222' }
    },
    setOrders(state, response) {
      state.order = {
        data: [...state.order.data, ...response.data],
        currentPage: response.currentPage,
        total: response.total
      }
    }
  },
  actions: {
    async fetchOrders(context, params) {
      const result = await axios({
        method: 'POST',
        headers: {
          Authorization: 'Bearer 789fbd8a-fbcb-434e-bb45-b23fa15b80d0'
        },
        url: 'http://192.168.30.166:9999/c/orders/consigner/search',
        data: params,
      })
      if (result && result.data && result.data.code === '0') {
        const data = result.data.data
        context.commit('setOrders', {
          data: data.records,
          currentPage: data.current,
          total: data.total,
        })
      }
    }
  },
  getters: {
    getterOrders(state) {
      return state.order.data
    }
  },
  modules: {
  }
})
